import {
	useHandleType,
	useGetEventGrid,
	useGetEventRole,
	useGetEventDept,
	useGetEventUser,
	useGetEventGrandson,
	useGetDutyDept,
	useGetDeptUsers
} from "../../../report/hooks/useEventNextHandler"
import {
	HandleMap
} from "@/enums/report"
import {
	LoadEnum
} from "../../../../../components/wakin-cascader/config"
import {
	toHump
} from "../../../../../common/utils"

export default {
	data() {
		return {
			nextHandlerShow: false,
			mfCheckPath: "",
			taskId: "",
			parents: null,
			mfEventHandler: null, // 选择的下一节点处理人id
			hasDept: false, // 有"按权责部门"
			deptData: [] // 权责部门信息
		}
	},
	mounted() {
		this.$nextTick(() => {
			this.mfGetDutyDept()
		})
	},
	methods: {
		// 查询所属权责部门
		mfGetDutyDept() {
			const param = {
				bs: this.eventSubclass ? 2 : 1,
				eventTypeId: this.eventTypeId
			}
			useGetDutyDept(param).then(res => {
				if (res.code === 200 && res.data && res.data.length > 0) {
					this.hasDept = true
					// this.deptData = res.data;
					res.data[0].children.map(m => m.loadEnum = LoadEnum.async);
					this.deptData = [{
						label: `权责部门:${res.data[0].deptName}`, // "deptName",
						source: res.data[0].children,
						value: HandleMap.dept,
						loadEnum: LoadEnum.async
					}]
				} else {
					this.hasDept = false;
					this.deptData = [];
				}
			});
		},
		async mfCheckHandler() {
			if (uni.$u.test.empty(this.taskId)) {
				this.$toast("请选择带有流程的事件类型")
				return;
			}
			if (uni.$u.test.empty(this.lat)) {
				this.$toast("请选择纬度")
				return;
			}
			if (uni.$u.test.empty(this.lon)) {
				this.$toast("请选择经度")
				return;
			}
			if (uni.$u.test.empty(this.instanceId)) {
				this.$toast("请选择流程id")
				return;
			}
			if (!this.parents) {
				const {
					code,
					data = null
				} = await useHandleType({
					taskId: this.taskId,
					lat: this.lat,
					lon: this.lon,
					instanceId: this.instanceId
				}, 1)
				if (code === 200) {
					// debugger
					data[0].LoadEnum = LoadEnum.tree;
					this.parents = data;
					// this.$refs.RefNextHandleCascader.push([...data, ...this.deptData], this.mfVerify);
				}
				this.$refs.RefNextHandleCascader.push([...this.parents, ...this.deptData], this.mfVerify);
			}
			this.nextHandlerShow = true;
		},
		/**
		 * @description 下一节点处理人级联组件点选回调
		 * @param {Number} level 当前选择的层级
		 * @param {Object} e 当前选择的数据对象
		 * 
		 */
		async mfCheckEventConfirm({
			level,
			e,
			navs
		}) {
			let list = [];
			const parent = navs[0];
			if (e.source && Array.isArray(e.source)) {
				e.source.forEach(f => {
					Object.keys(f).map(key => this.$set(f, toHump(key), f[key]));
				})
			} else {
				Object.keys(e).map(key => this.$set(e, toHump(key), e[key]));
			}
			const loadEnum = Array.isArray(e.source) ? e.source[0].loadEnum : e.loadEnum; // 点击了权责部门分支
			if (level === 0) {
				switch (e.value) {
					case HandleMap.grid:
						list = await useGetEventGrid(e.source)
						break;
					case HandleMap.role:
						list = await useGetEventRole(e.source)
						break;
					case HandleMap.dept:
						list = await useGetEventDept(e.source)
						break;
					case HandleMap.user:
						list = await useGetEventUser(e.source)
						break;
					default:
						break;
				}
			}
			// 点击了权责部门分支
			else if (loadEnum === LoadEnum.async) {
				const {
					code,
					data
				} = await useGetDeptUsers({
					deptId: e.deptId
				})
				list = data ?? [];
			} else if (parent.value !== HandleMap.user) {
				const {
					code,
					data
				} = await useGetEventGrandson({
					bs: parent.value,
					id: e.value
				})
				list = data ?? []
				console.log("子孙节点", data);
			}
			this.$refs.RefNextHandleCascader.push(list, this.mfVerify);
		},
		mfVerify(level, array) {
			if (level === 0 && (!array || array.length === 0)) {
				this.$toast("当前节点下无可选数据")
				return false;
			}
			return true
		},
		mfChooseConfirm({
			chooseItem,
			choosePath,
			path,
			level
		}) {
			if (level === 0) {
				this.$toast("当前节点下无可选数据");
				return;
			}
			const parent = choosePath[0];
			// this.form[formKeys.eventHandler] = chooseItem.user_id?.toString();
			// this.form[formKeys.eventType] = parent.value;
			this.mfEventHandler = chooseItem.user_id?.toString() ?? chooseItem.userId?.toString()
			this.mfCheckPath = path;
			console.log("mfChooseConfirm", chooseItem, choosePath);
		}
	}
}



/**
 * 一.网格 taskDealType: "1"
 *    数据源:taskData.data数组 [{grid_id:数据id,grid_name:数据名称}]
 * 二.角色 taskDealType: "2"
 *    数据源:taskData.data数组 [{roleId:数据id,roleName:数据名称}]
 * 三.部门 taskDealType: "3"
 *   数据源:taskData.data数组 [{deptId:数据id,deptName:数据名称}]
 * 四.人员 taskDealType: "4"  不用再请求了
 *   数据源:taskData.data数组 [{user_id:数据id,user_name:数据名称}]
 */